package com.lwl.blue.commont.utils.ancDec;

import java.math.BigDecimal;

/**
 * <p>  </p>
 *
 * @Date: 2021/11/22 11:27
 * @version: V1.0
 * @Decription:
 * @author: 蓝文良
 */
public class NumberCrypt {
    private final static BigDecimal mulOne = new BigDecimal(6598);
    private final static BigDecimal addOne = new BigDecimal(98656);
    private final static BigDecimal subOne = new BigDecimal(654);
    private final static BigDecimal mulTwo = new BigDecimal(56);
    private final static BigDecimal subTwo = new BigDecimal(654);
    private final static int defaultDigit = 2;

    /**
     * 加密
     *
     * @param number
     * @return
     */
    public static BigDecimal encrypt(BigDecimal number) {
        return encrypt(number, defaultDigit);
    }

    /**
     * 解密
     *
     * @param number
     * @return
     */
    public static BigDecimal decrypt(BigDecimal number) {
        return decrypt(number, defaultDigit);
    }

    /**
     * 加密
     *
     * @param number
     * @return
     */
    public static BigDecimal encrypt(BigDecimal number, Integer digit) {
        if (null == digit) {
            digit = defaultDigit;
        }
        BigDecimal re = number.multiply(mulOne)
                .add(addOne)
                .subtract(subOne)
                .multiply(mulTwo)
                .subtract(subTwo).setScale(digit, BigDecimal.ROUND_HALF_UP);

        return re;
    }

    /**
     * 解密
     *
     * @param number
     * @return
     */
    public static BigDecimal decrypt(BigDecimal number, Integer digit) {
        if (null == digit) {
            digit = defaultDigit;
        }
        BigDecimal re = number.add(subTwo)
                .divide(mulTwo)
                .add(subOne)
                .subtract(addOne)
                .divide(mulOne)
                .setScale(digit, BigDecimal.ROUND_HALF_UP);

        return re;
    }
}
